function render(res,data,type='') {
  res.writeHead(200,{"Content-Type":`${type?type:"application/json"};charset=utf-8`})
  console.log(data)
  res.write(data)
  res.end()
}


const apiRouter = {
  '/api/login':(req,res) => {
    const myUrl = new URL(req.url,'http://127.0.0.1')
    console.log(myUrl.searchParams)
    if(myUrl.searchParams.get('username') == 'zy' && myUrl.searchParams.get('password') == '123456') {
      render(res,JSON.stringify({ok:1}))
    } else {
      render(res,JSON.stringify({ok:0}))
    }
  },

  '/api/loginpost':(req,res) => {
    // render(res,JSON.stringify({ok:0}))

    let post = ''
    req.on('data',chunk => {
      post += chunk
    })
    req.on('end',() => {
      console.log(post)
      post = JSON.parse(post)
      if(post.username == 'zy' && post.password == '123456') {
        render(res,`{"ok":1}`)
      } else {
        render(res,`{"ok":0}`)

      }
    })
  }
}

module.exports = apiRouter